package com.wing.productionPlan.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.productionPlan.enumeration.AuditStatusEnum;
import com.wing.productionPlan.model.entity.RequirePlanItem;
import com.wing.productionPlan.model.response.RequirePlanItermVO;
import com.wing.productionPlan.model.response.RequirePlanVO;
import com.wing.productionPlan.service.RequirePlanItermService;
import com.wing.productionPlan.service.RequirePlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Validated
@Api(value = "Admin-生产管理-需求计划明细", tags = "#/product/requirePlan/requirePlanItermList")
@RestController("RequirePlanItermController")
@RequestMapping("productionPlan/admin/requirePlanIterm")
@RequiredArgsConstructor
public class RequirePlanItermController {

    private final RequirePlanService requirePlanService;

    private final RequirePlanItermService requirePlanItermService;


    @ApiOperation(value = "查询需求计划明细列表", notes = "搜索-任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<RequirePlanItermVO> selectPlanItermList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String factoryName
            ,String purchaseTissue
            ,String materialCode
            ,String startTime
            ,String endTime
    ) {
        Page<RequirePlanItermVO> pg = new Page<>(page, limit);
        HashMap map = new HashMap();
        map.put("purchaseTissue", purchaseTissue);
        map.put("factoryName", factoryName);
        map.put("materialCode", materialCode);
        map.put("startTime", startTime);
        map.put("endTime", endTime);

        Page<RequirePlanItermVO> list = requirePlanItermService.selectPlanItermList(pg, map);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "查询需求计划明细详情", notes = "搜索")
    @GetMapping("/getPlanItermDetail")
    public JsonResult getPlanItermDetail(@RequestParam("id") Long id,@RequestParam("planId") Long planId) {
        RequirePlanVO appById = requirePlanService.get4appById(planId);
        if(appById!=null){
            appById.setAuditStatusText(AuditStatusEnum.getDescriptionByCode(appById.getAuditStatus()));
            RequirePlanItermVO planItermById = requirePlanItermService.getPlanItermById(id);
            List<RequirePlanItem>  requirePlanItemList=new ArrayList<>();
            RequirePlanItem requirePlanItem=new RequirePlanItem();
            BeanUtil.copyProperties(planItermById, requirePlanItem,true);
            requirePlanItemList.add(requirePlanItem);
            appById.setRequirePlanItemList(requirePlanItemList);
            return JsonResult.success(appById);
        }else{
            return JsonResult.fail("未发现详情数据！");
        }
    }




}
